﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


//using Microsoft.AspNet.Identity.EntityFramework;

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Reflection;
using Almacen.Data.Entities;

//using Almacen.Data.Managers;

namespace Almacen.Data
{
    public class AlmacenDbContext : DbContext
    {
        public AlmacenDbContext()
            : base("DefaultConnection")
        {

        }

        static AlmacenDbContext()
        {
            // Set the database intializer which is run once during application start
        }
        


        public static AlmacenDbContext Create()
        {
            return new AlmacenDbContext();
        }

        public DbSet<MeasureUnit> MeasureUnits { get; set; }
        public DbSet<Product> Products { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
           .Where(type => !String.IsNullOrEmpty(type.Namespace))
           .Where(type => type.BaseType != null && type.BaseType.IsGenericType
                && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
            foreach (var type in typesToRegister)
            {
                dynamic configurationInstance = Activator.CreateInstance(type);
                modelBuilder.Configurations.Add(configurationInstance);
            }
            base.OnModelCreating(modelBuilder);
        }


    }
}
